<?php
/*****************************************************************************
**	Файл:	Активация учетной записи пользователя							**
**	Автор:	Dark Dayver														**
**	Версия:	1.0b															**
**	Дата:	25/12-2009														**
*****************************************************************************/
if (IN_P_ENIGMA)
{
	die('HACK!');
}

if (SERVER_NO)
{
	if ($work->check_get('resend', true, true))
	{
		if ($work->check_get('login', true, true, REG_LOGIN) && $work->check_get('email', true, true, REG_EMAIL))
		{
			$db->select(array('id', 'name'), DB_USER, TBL_USERS);
			$db->where('`login` = "' . $_GET['login'] . '" AND `email` = "' . $_GET['email'] . '"');
			$db->query();
			$tmp = $db->res_row();
			$db->clean();
			if ($tmp)
			{
				$mail_array = array();
				$activated_code = $user->gen_activated_code($tmp['id']);
				$_SESSION['info'] = true;
				if ($activated_code)
				{
					$url_activated = $work->config['site_url'] . '?action=activate&login=' . $_GET['login'] . '&email=' . $_GET['email'];
					$mail_array['USER_NAME'] = $tmp['name'];
					$mail_array['USER_EMAIL'] = $_GET['email'];
					$mail_array['URL_ACCOUNT_ACTIVATE'] = $url_activated . '&activated_code=' . $activated_code;
					$mail_array['URL_ACTIVATE'] = $url_activated;
					$mail_array['USER_LOGIN'] = $_GET['login'];
					$mail_array['USER_CODE_ACTIVATED'] = $activated_code;
					$mail_array['USER_PASS'] = '(********)';
					$mail_array['SERVER_URL'] = $work->config['site_url'];
					$mail_array['SERVER_NAME'] = $work->config['title'];
					$language->local_mail($_GET['email'], $_GET['login'], 'confirm_register', $mail_array);
					$_SESSION['info_txt'] = sprintf($language->main['resend_activated_code'], $_GET['email']);
				}
				else
				{
					$this->db->update(array('activated_code' => NULL, 'activate' => '1'), DB_USER, TBL_USERS);
					$this->db->where('`id` = ' . $tmp['id']);
					$this->db->query();
					$this->db->clean();
					$_SESSION['info_txt'] = sprintf($language->main['activated_user'], $tmp['name'], $work->filt_email($_GET['email']));
				}
			}
			else
			{
				$_SESSION['error'] = true;
				$_SESSION['error_txt'] = $language->error['no_this_user'];
			}
		}
		else
		{
			$_SESSION['error'] = true;
			$_SESSION['error_txt'] = $language->error['unknown'];
		}
		$user->last_active();
		header('Location: ' . $work->config['site_url']);
		die('HACK!');
	}
	$activated = false;
	if ($work->check_get('login', true, true, REG_LOGIN) && $work->check_get('email', true, true, REG_EMAIL) && $work->check_get('activated_code', true, true))
	{
		$activated = $user->activated_user($_GET['login'], $_GET['email'], $_GET['activated_code']);
	}
	if ($activated)
	{
		$mail_array['USER_NAME'] = $activated['name'];
		$mail_array['USER_EMAIL'] = $_GET['email'];
		$mail_array['USER_LOGIN'] = $_GET['login'];
		$mail_array['USER_PASS'] = '(********)';
		$mail_array['SERVER_URL'] = $work->config['site_url'];
		$mail_array['SERVER_NAME'] = $work->config['title'];
		$language->local_mail($_GET['email'], $_GET['login'], 'register', $mail_array);
		$_SESSION['user_login'] = $_GET['login'];
		if ($work->config['notify_admin_of_regist'])
		{
			$language->local_mail($work->config['admin_email'], 'Admin', 'notify_admin_new_regist', $mail_array);
		}
		$user->last_active();
		header('Location: ' . $work->config['site_url']);
		die('HACK!');
	}
	else
	{
		$template->template_file = 'activate.html';
		$title .= ' - ' . $language->title['activate'];
		$template->array_data['L_SERVER_NAME'] = $work->config['description'];
		$template->array_data['L_INPUT_ACTIVATE'] = $language->main['input_activate'];
		$template->array_data['L_LOGIN'] = $language->main['user_login'];
		$template->array_data['L_EMAIL'] = $language->main['user_email'];
		$template->array_data['L_ACTIVATE_CODE'] = $language->main['user_activated_code'];
		$template->array_data['L_ACTIVATE_USER'] = $language->main['activate_login'];
		$template->array_data['U_ACTIVATE'] = $work->config['site_url'];
		$template->array_data['D_LOGIN'] = '';
		$template->array_data['D_EMAIL'] = '';
		if ($work->check_get('login', true, true, REG_LOGIN))
		{
			$template->array_data['D_LOGIN'] = $_GET['login'];
		}
		if ($work->check_get('email', true, true, REG_EMAIL))
		{
			$template->array_data['D_EMAIL'] = $_GET['email'];
		}
	}
}
else
{
	$_SESSION['error'] = true;
	$_SESSION['error_txt'] = $language->error['no_out_server'];
	$user->last_active();
	header('Location: ' . $work->config['site_url']);
	die('HACK!');
}
?>
